package cc.mcggdd.leah.boot.stock.service.impl;

import cc.mcggdd.leah.boot.core.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cc.mcggdd.leah.boot.stock.mapper.StockInitCapitalMapper;
import cc.mcggdd.leah.boot.stock.service.StockInitCapitalService;
import cc.mcggdd.leah.boot.stock.model.entity.StockInitCapital;
import cc.mcggdd.leah.boot.stock.model.form.StockInitCapitalForm;
import cc.mcggdd.leah.boot.stock.model.query.StockInitCapitalQuery;
import cc.mcggdd.leah.boot.stock.model.vo.StockInitCapitalVO;
import cc.mcggdd.leah.boot.stock.converter.StockInitCapitalConverter;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 初始资金服务实现类
 *
 * @author 
 * @since 2025-02-08 10:16
 */
@Service
@RequiredArgsConstructor
public class StockInitCapitalServiceImpl extends ServiceImpl<StockInitCapitalMapper, StockInitCapital> implements StockInitCapitalService {

    private final StockInitCapitalConverter stockInitCapitalConverter;

    /**
    * 获取初始资金分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<StockInitCapitalVO>} 初始资金分页列表
    */
    @Override
    public IPage<StockInitCapitalVO> getStockInitCapitalPage(StockInitCapitalQuery queryParams) {
        Page<StockInitCapitalVO> pageVO = this.baseMapper.getStockInitCapitalPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取初始资金表单数据
     *
     * @param id 初始资金ID
     * @return
     */
    @Override
    public StockInitCapitalForm getStockInitCapitalFormData(Long id) {
        StockInitCapital entity = this.getById(id);
        return stockInitCapitalConverter.toForm(entity);
    }
    
    /**
     * 新增初始资金
     *
     * @param formData 初始资金表单对象
     * @return
     */
    @Override
    public boolean saveStockInitCapital(StockInitCapitalForm formData) {
        StockInitCapital entity = stockInitCapitalConverter.toEntity(formData);
        entity.setCreateBy(SecurityUtils.getUserId());
        return this.save(entity);
    }
    
    /**
     * 更新初始资金
     *
     * @param id   初始资金ID
     * @param formData 初始资金表单对象
     * @return
     */
    @Override
    public boolean updateStockInitCapital(Long id,StockInitCapitalForm formData) {
        StockInitCapital entity = stockInitCapitalConverter.toEntity(formData);
        entity.setUpdateBy(SecurityUtils.getUserId());
        return this.updateById(entity);
    }
    
    /**
     * 删除初始资金
     *
     * @param ids 初始资金ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteStockInitCapitals(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的初始资金数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public BigDecimal getStockInitCapitalsTotalAmount() {
        return this.baseMapper.getStockInitCapitalsTotalAmount();
    }

}
